/** *******************************************************************
 * FlexDatabaseManager
 * @author  Jonnie Spratley
 * @contact jonniespratley@gmail.com
 * @internal
 ******************************************************************* */
package com.jonniespratley.flexdatabasemanager.commands.backups
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.jonniespratley.flexdatabasemanager.business.FlexDatabaseManagerDelegate;
	import com.jonniespratley.flexdatabasemanager.events.backups.RemoveBackupEvent;
	import com.jonniespratley.flexdatabasemanager.model.ModelLocator;
	import com.jonniespratley.flexdatabasemanager.vo.*;
	
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;

	public class RemoveBackupCommand implements ICommand, IResponder
	{
		private var model:ModelLocator = ModelLocator.getInstance();

		public function execute( event:CairngormEvent ) : void
		{
			model.interactingWithServer = true;
			
			var evt:RemoveBackupEvent = event as RemoveBackupEvent;
			
			if ( model.selectedDatabase == null || evt.aBackup == null )
			{
				Alert.show( 'Please select a file to remove' );
			} else {
			var delegate:FlexDatabaseManagerDelegate = new FlexDatabaseManagerDelegate( this );
				delegate.removeBackup( model.currentUser, model.selectedDatabase, evt.aBackup );
			}
		}
		
		public function result( data:Object ) : void
		{
			model.interactingWithServer = false;
			
			trace( data.result );
		}

		public function fault( fault:Object ):void
		{
			model.interactingWithServer = false;
			
			var faultEvt:FaultEvent = fault as FaultEvent;
			Alert.show(faultEvt.fault.toString(), "Error");
			trace(faultEvt.fault.faultString);
		}
	}
}